// pages/minehistory/minehistory.js
var shihan = require('../../../common/fetch');
var config = require('../../../common/config');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    page_size: 20,
    historylistflag: true,
    showLoading: true,
    all_history_list: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.refreshhistorylist()
  },
  refreshhistorylist: function() {
    var that = this
    shihan.sendRequest(config.get_product_history, 'POST', { page: that.data.page, page_size: that.data.page_size })
      .then(function(response) {
        if (response.data.code == 0) {
          if (response.data.data.sh_total_rows) {
            that.setData({
              listRows: response.data.data.sh_total_rows
            })
          }
          if (response.data.data.sh_items.length > 0) {
            that.setData({
              page: that.data.page + 1,
              historyinfo: response.data.data,
              all_history_list: that.data.all_history_list.concat(response.data.data.sh_items),
              hasMore: true,
              historylistflag: true,
              showLoading: false
            })
          } else {
            that.setData({
              hasMore: false,
              historylistflag: false,
              showLoading: false
            })
          }
          if (that.data.listRows == that.data.all_history_list.length) {
            that.setData({
              hasMore: false,
              historylistflag: false
            })
          }
          wx.stopPullDownRefresh()
        } else {
          that.setData({
            hasMore: false,
            historylistflag: false,
            showLoading: false
          })
        }
      }, function(error) {
        console.log(error);
      });
  },
  viewgoodsDetail: function(e) {
    wx.navigateTo({
      url: '/pages/goodsdetail/goodsdetail?goodsid=' + e.currentTarget.dataset.id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.setData({
      page: 1,
      all_history_list: [],
      showLoading: true
    });
    if (this.data.all_history_list == 0) {
      this.refreshhistorylist()
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.historylistflag) {
      this.setData({
        historylistflag: false
      })
      this.refreshhistorylist()
    }
  }
})